/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package net.risesoft.risebi.collection.web.client;

import java.util.HashMap;
import java.util.List;

import net.risesoft.risebi.collection.model.Catalog;
import net.risesoft.risebi.collection.model.Module;
import net.risesoft.risebi.collection.model.Service;
import net.risesoft.risebi.collection.model.SoType;
import net.risesoft.risebi.collection.model.User;
import net.risesoft.risebi.collection.model.UserSO;
import net.risesoft.risebi.collection.service.DynaBeanManager;
import net.risesoft.risebi.collection.service.ModuleManager;
import net.risesoft.risebi.collection.service.ShortMessageManager;
import net.risesoft.risebi.collection.service.SoTypeManager;
import net.risesoft.risebi.collection.service.UserSOManager;
import net.risesoft.risebi.collection.web.BaseAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 *
 */
@Namespace("/client/IndexAction")
public class IndexAction extends BaseAction {
	private static final long serialVersionUID = 7353595559801055736L;
	private final Log log = LogFactory.getLog(getClass());
	
	private ModuleManager moduleManager;
	private UserSOManager userSOManager;
	private SoTypeManager soTypeManager;
	private DynaBeanManager dynaBeanManager;
	private ShortMessageManager shortMessageManager;
	
	private String username;
	private String userSo_list = "";
	
	private int newInboxCount;
	private String initialPassword;
	private String newShortMessage;
	
	private Service service;

	public Service getService() {
		return service;
	}

	public void setService(Service service) {
		this.service = service;
	}

	@Action(value="index",
			results={@Result(name="success", location="/client/index.jsp")}
	)
    public String index() throws Exception {
    	User user = this.getSessionUser();
    	this.username = user.getName();
    	List<UserSO> list = userSOManager.getUserSOByUid(user.getGuid());
    	for(UserSO userSO : list){
    		Catalog so = userSO.getSo();
    		String appName = so.getApplication().getChineseName();
    		SoType soType = (SoType)soTypeManager.getObject(SoType.class, so.getSoTypeGuid());
     		HashMap<String,Object> dynaBean = (HashMap<String,Object>)dynaBeanManager.getObject(so.getTableName().trim().toLowerCase(), userSO.getSoInstanceGuid());
     		String soi_name = (String)dynaBean.get("item_name");     		
     		if(userSo_list.trim().length()==0){
     			//userSo_list = soi_name + "【" + soType.getName() + "，" + appName + "】";
     			userSo_list = soi_name;
     		}else{
     			//userSo_list = soi_name + "【" + soType.getName() + "，" + appName + "】" + "<br />" + userSo_list;
     			userSo_list = soi_name + "，" + userSo_list;
     		}
    	}
    	
//    	一般来说，企业用户只有一个服务
    	UserSO userSo = list.get(0);
    	Catalog so = userSo.getSo();
    	service = so.getService();
    	
//    	获得收件箱新的短信
    	newInboxCount = shortMessageManager.getInboxCountByUserGuid(user.getGuid(),false);
    	
    	if(newInboxCount>0){
    		newShortMessage = "<a href='javascript:void(0)' class='a02' onclick='gotoInbox();'>您有新的短信：<span style='color:red' id='newShortMessageCount'> "+newInboxCount+"</span>条<img id='newShortMessageImage' src='"+this.getContextPath()+"/images/system/newpm.gif'></a>，";
    	}else{
    		newShortMessage = "<a href='javascript:void(0)' class='a02' onclick='gotoInbox();'>您有新的短信：<span style='color:red'> "+newInboxCount+"</span>条</a>，";
    	}
    	
//    	判断是否需要更改密码
    	if(user.judgeInitialPassword()){
    		initialPassword = "<span class='mysize' id='initialPasswordDiv'><img src='"+this.getContextPath()+"/images/system/beep.gif' height='14'><a href='javascript:void(0)' class='a01' onclick='changePs();'>密码太简单，建议您修改</a></span>";
    	}
    	
        return SUCCESS;
    }
    
	@Action(value="blank",
			results={@Result(name="success", location="/blank.jsp")}
	)
    public String blank() throws Exception {
    	return SUCCESS;
    }
    
	@Action(value="welcome",
			results={@Result(name="success", location="/welcome.jsp")}
			)
	public String welcome() throws Exception {
		return SUCCESS;
	}
	
	@Action(value="getModuleTreeNodes")
	public String getModuleTreeNodes() throws Exception {
		try{
			List<Module> list = null;
			if("moduleTreeRootNode".equals(this.node)){
				List<Module> modules = moduleManager.find("from Module m where m.name='企业填报' ");
				if(modules.size()==0){
					this.json = "[]";
					return AJAX;
				}
				list = moduleManager.getModulesByGuid(modules.get(0).getGuid());
			}else{
				list = moduleManager.getModulesByGuid(this.node);
			}				
				
			JSONArray jsonArray = new JSONArray();
			for(Module t : list){
				boolean isleaf = true;
				int count = moduleManager.getCount("select count(*) from Module t where t.parentModule.guid = '" + t.getGuid() + "'");
				if(count>0){
					isleaf = false;
				}
				
				JSONObject jsonObj = new JSONObject();
				jsonObj.element("id", t.getGuid());
				jsonObj.element("text", t.getName());
				jsonObj.element("url", t.getUrl());
				jsonObj.element("expanded", true);
				jsonObj.element("singleClickExpand", true);
				jsonObj.element("qtip", "");
				jsonObj.element("iconCls", t.getIconName());
				jsonObj.element("disabled", !t.getEnabled());
				jsonObj.element("leaf", isleaf);
				jsonArray.add(jsonObj);
			}	
			this.json = jsonArray.toString();
		}catch(Exception e){
			this.json = "[]";
			e.printStackTrace();
		} 
		
		return AJAX;
	}
	
	public String getNewShortMessage() {
		return newShortMessage;
	}

	public void setNewShortMessage(String newShortMessage) {
		this.newShortMessage = newShortMessage;
	}

	public String getInitialPassword() {
		return initialPassword;
	}

	public void setInitialPassword(String initialPassword) {
		this.initialPassword = initialPassword;
	}

	public int getNewInboxCount() {
		return newInboxCount;
	}

	public void setNewInboxCount(int newInboxCount) {
		this.newInboxCount = newInboxCount;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public ModuleManager getModuleManager() {
		return moduleManager;
	}

	public void setModuleManager(ModuleManager moduleManager) {
		this.moduleManager = moduleManager;
	}

	public UserSOManager getUserSOManager() {
		return userSOManager;
	}

	public void setUserSOManager(UserSOManager userSOManager) {
		this.userSOManager = userSOManager;
	}

	public DynaBeanManager getDynaBeanManager() {
		return dynaBeanManager;
	}

	public void setDynaBeanManager(DynaBeanManager dynaBeanManager) {
		this.dynaBeanManager = dynaBeanManager;
	}

	public SoTypeManager getSoTypeManager() {
		return soTypeManager;
	}

	public void setSoTypeManager(SoTypeManager soTypeManager) {
		this.soTypeManager = soTypeManager;
	}

	public String getUserSo_list() {
		return userSo_list;
	}

	public void setUserSo_list(String userSo_list) {
		this.userSo_list = userSo_list;
	}
    
	public void setShortMessageManager(ShortMessageManager shortMessageManager) {
		this.shortMessageManager = shortMessageManager;
	}
    
}
